########################################################
## PROGRAM NAME: 007_traj.R                           ##
## AUTHOR: MATT MLECZKO                               ##
## DATE CREATED: 04/28/2021                           ##
## INPUTS:                                            ##
##    002_af_wide_final.Rda                           ##
##                                                    ##
## OUTPUTS:                                           ##
##                                                    ## 
## PURPOSE: Create Figure 4                           ##
##                                                    ##
## LIST OF UPDATES:                                   ##
##                                                    ##
########################################################

log <- file("006_pt.txt") 
sink(log, append=TRUE)
sink(log, append=TRUE, type="message")

set.seed(08540)

## load libraries ##

library(tidycensus)
library(tidyverse)
library(dplyr)
library(foreign)
library(stringr)
library(tm)
library(gdata)
library(gsubfn)
library(readxl)
library(stargazer)
library(ggplot2)
library(haven)

## define paths ##
data_path <- "PATH TO DATA HERE"
progs <- "PATH TO PROGRAMS HERE"

## set working directory ##
setwd(data_path)

##
## load necessary functions ## 
##

`%notin%` <- Negate(`%in%`)
source(paste0(progs,"stata_merge.R"))

## load the analytic file ##
load("002_af_wide_final.Rda")

## let's do groupings ## 

## 2000 ##

groups.2000 <- af.wide.final %>%
  mutate(group = case_when(## asian ## 
                           maj_race_2000 == "per_asian" & 
                           maj_ses_2000=="vli" & 
                           int_jt_relf_a_2000 == 1 ~ "int_asian_vli",
                           maj_race_2000 == "per_asian" & 
                           maj_ses_2000=="linc" & 
                           int_jt_relf_a_2000 == 1 ~ "int_asian_li",
                           maj_race_2000 == "per_asian" & 
                           maj_ses_2000=="linc" & 
                           int_jt_relf_a_2000 == 1 ~ "int_asian_li",
                           maj_race_2000 == "per_asian" & 
                           maj_ses_2000=="minc" & 
                           int_jt_relf_a_2000 == 1 ~ "int_asian_mi",
                           maj_race_2000 == "per_asian" & 
                             maj_ses_2000=="hmi" & 
                             int_jt_relf_a_2000 == 1 ~ "int_asian_hmi",
                           maj_race_2000 == "per_asian" & 
                             maj_ses_2000=="hinc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_asian_hi",
                           maj_race_2000 == "per_asian" & 
                             maj_ses_2000=="vhi" & 
                             int_jt_relf_a_2000 == 1 ~ "int_asian_vhi",
                           maj_race_2000 == "per_asian" & 
                             maj_ses_2000=="vli" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_asian_vli",
                           maj_race_2000 == "per_asian" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_asian_li",
                           maj_race_2000 == "per_asian" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_asian_li",
                           maj_race_2000 == "per_asian" & 
                             maj_ses_2000=="minc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_asian_mi",
                           maj_race_2000 == "per_asian" & 
                             maj_ses_2000=="hmi" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_asian_hmi",
                           maj_race_2000 == "per_asian" & 
                             maj_ses_2000=="hinc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_asian_hi",
                           maj_race_2000 == "per_asian" & 
                             maj_ses_2000=="vhi" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_asian_vhi",
                           ## black ## 
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="vli" & 
                             int_jt_relf_a_2000 == 1 ~ "int_black_vli",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_black_li",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_black_li",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="minc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_black_mi",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="hmi" & 
                             int_jt_relf_a_2000 == 1 ~ "int_black_hmi",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="hinc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_black_hi",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="vhi" & 
                             int_jt_relf_a_2000 == 1 ~ "int_black_vhi",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="vli" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_black_vli",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_black_li",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_black_li",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="minc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_black_mi",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="hmi" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_black_hmi",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="hinc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_black_hi",
                           maj_race_2000 == "per_black" & 
                             maj_ses_2000=="vhi" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_black_vhi",
                           ## latino ## 
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="vli" & 
                             int_jt_relf_a_2000 == 1 ~ "int_hl_vli",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_hl_li",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_hl_li",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="minc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_hl_mi",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="hmi" & 
                             int_jt_relf_a_2000 == 1 ~ "int_hl_hmi",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="hinc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_hl_hi",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="vhi" & 
                             int_jt_relf_a_2000 == 1 ~ "int_hl_vhi",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="vli" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_hl_vli",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_hl_li",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_hl_li",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="minc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_hl_mi",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="hmi" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_hl_hmi",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="hinc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_hl_hi",
                           maj_race_2000 == "per_hl" & 
                             maj_ses_2000=="vhi" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_hl_vhi",
                           ## white ## 
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="vli" & 
                             int_jt_relf_a_2000 == 1 ~ "int_white_vli",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_white_li",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_white_li",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="minc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_white_mi",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="hmi" & 
                             int_jt_relf_a_2000 == 1 ~ "int_white_hmi",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="hinc" & 
                             int_jt_relf_a_2000 == 1 ~ "int_white_hi",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="vhi" & 
                             int_jt_relf_a_2000 == 1 ~ "int_white_vhi",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="vli" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_white_vli",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_white_li",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="linc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_white_li",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="minc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_white_mi",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="hmi" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_white_hmi",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="hinc" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_white_hi",
                           maj_race_2000 == "per_white" & 
                             maj_ses_2000=="vhi" & 
                             int_jt_relf_a_2000 == 0 ~ "seg_white_vhi")) %>%
  group_by(int_jt_relf_a_2000, group) %>%
  summarize(n = n())

## 2010 ##

groups.2010 <- af.wide.final %>%
  mutate(group = case_when(## asian ## 
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="vli" & 
      int_jt_relf_a_2010 == 1 ~ "int_asian_vli",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 1 ~ "int_asian_li",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 1 ~ "int_asian_li",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="minc" & 
      int_jt_relf_a_2010 == 1 ~ "int_asian_mi",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="hmi" & 
      int_jt_relf_a_2010 == 1 ~ "int_asian_hmi",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="hinc" & 
      int_jt_relf_a_2010 == 1 ~ "int_asian_hi",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="vhi" & 
      int_jt_relf_a_2010 == 1 ~ "int_asian_vhi",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="vli" & 
      int_jt_relf_a_2010 == 0 ~ "seg_asian_vli",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_asian_li",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_asian_li",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="minc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_asian_mi",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="hmi" & 
      int_jt_relf_a_2010 == 0 ~ "seg_asian_hmi",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="hinc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_asian_hi",
    maj_race_2010 == "per_asian" & 
      maj_ses_2010=="vhi" & 
      int_jt_relf_a_2010 == 0 ~ "seg_asian_vhi",
    ## black ## 
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="vli" & 
      int_jt_relf_a_2010 == 1 ~ "int_black_vli",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 1 ~ "int_black_li",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 1 ~ "int_black_li",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="minc" & 
      int_jt_relf_a_2010 == 1 ~ "int_black_mi",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="hmi" & 
      int_jt_relf_a_2010 == 1 ~ "int_black_hmi",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="hinc" & 
      int_jt_relf_a_2010 == 1 ~ "int_black_hi",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="vhi" & 
      int_jt_relf_a_2010 == 1 ~ "int_black_vhi",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="vli" & 
      int_jt_relf_a_2010 == 0 ~ "seg_black_vli",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_black_li",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_black_li",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="minc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_black_mi",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="hmi" & 
      int_jt_relf_a_2010 == 0 ~ "seg_black_hmi",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="hinc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_black_hi",
    maj_race_2010 == "per_black" & 
      maj_ses_2010=="vhi" & 
      int_jt_relf_a_2010 == 0 ~ "seg_black_vhi",
    ## latino ## 
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="vli" & 
      int_jt_relf_a_2010 == 1 ~ "int_hl_vli",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 1 ~ "int_hl_li",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 1 ~ "int_hl_li",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="minc" & 
      int_jt_relf_a_2010 == 1 ~ "int_hl_mi",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="hmi" & 
      int_jt_relf_a_2010 == 1 ~ "int_hl_hmi",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="hinc" & 
      int_jt_relf_a_2010 == 1 ~ "int_hl_hi",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="vhi" & 
      int_jt_relf_a_2010 == 1 ~ "int_hl_vhi",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="vli" & 
      int_jt_relf_a_2010 == 0 ~ "seg_hl_vli",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_hl_li",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_hl_li",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="minc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_hl_mi",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="hmi" & 
      int_jt_relf_a_2010 == 0 ~ "seg_hl_hmi",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="hinc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_hl_hi",
    maj_race_2010 == "per_hl" & 
      maj_ses_2010=="vhi" & 
      int_jt_relf_a_2010 == 0 ~ "seg_hl_vhi",
    ## white ## 
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="vli" & 
      int_jt_relf_a_2010 == 1 ~ "int_white_vli",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 1 ~ "int_white_li",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 1 ~ "int_white_li",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="minc" & 
      int_jt_relf_a_2010 == 1 ~ "int_white_mi",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="hmi" & 
      int_jt_relf_a_2010 == 1 ~ "int_white_hmi",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="hinc" & 
      int_jt_relf_a_2010 == 1 ~ "int_white_hi",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="vhi" & 
      int_jt_relf_a_2010 == 1 ~ "int_white_vhi",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="vli" & 
      int_jt_relf_a_2010 == 0 ~ "seg_white_vli",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_white_li",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="linc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_white_li",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="minc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_white_mi",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="hmi" & 
      int_jt_relf_a_2010 == 0 ~ "seg_white_hmi",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="hinc" & 
      int_jt_relf_a_2010 == 0 ~ "seg_white_hi",
    maj_race_2010 == "per_white" & 
      maj_ses_2010=="vhi" & 
      int_jt_relf_a_2010 == 0 ~ "seg_white_vhi")) %>%
  group_by(int_jt_relf_a_2010, group) %>%
  summarize(n = n()) %>%
  mutate(share = n/nrow(af.wide.final) * 100)

## 2020 ##

groups.2020 <- af.wide.final %>%
  mutate(group = case_when(## asian ## 
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="vli" & 
      int_jt_relf_a_2020 == 1 ~ "int_asian_vli",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 1 ~ "int_asian_li",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 1 ~ "int_asian_li",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="minc" & 
      int_jt_relf_a_2020 == 1 ~ "int_asian_mi",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="hmi" & 
      int_jt_relf_a_2020 == 1 ~ "int_asian_hmi",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="hinc" & 
      int_jt_relf_a_2020 == 1 ~ "int_asian_hi",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="vhi" & 
      int_jt_relf_a_2020 == 1 ~ "int_asian_vhi",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="vli" & 
      int_jt_relf_a_2020 == 0 ~ "seg_asian_vli",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_asian_li",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_asian_li",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="minc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_asian_mi",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="hmi" & 
      int_jt_relf_a_2020 == 0 ~ "seg_asian_hmi",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="hinc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_asian_hi",
    maj_race_2020 == "per_asian" & 
      maj_ses_2020=="vhi" & 
      int_jt_relf_a_2020 == 0 ~ "seg_asian_vhi",
    ## black ## 
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="vli" & 
      int_jt_relf_a_2020 == 1 ~ "int_black_vli",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 1 ~ "int_black_li",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 1 ~ "int_black_li",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="minc" & 
      int_jt_relf_a_2020 == 1 ~ "int_black_mi",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="hmi" & 
      int_jt_relf_a_2020 == 1 ~ "int_black_hmi",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="hinc" & 
      int_jt_relf_a_2020 == 1 ~ "int_black_hi",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="vhi" & 
      int_jt_relf_a_2020 == 1 ~ "int_black_vhi",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="vli" & 
      int_jt_relf_a_2020 == 0 ~ "seg_black_vli",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_black_li",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_black_li",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="minc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_black_mi",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="hmi" & 
      int_jt_relf_a_2020 == 0 ~ "seg_black_hmi",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="hinc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_black_hi",
    maj_race_2020 == "per_black" & 
      maj_ses_2020=="vhi" & 
      int_jt_relf_a_2020 == 0 ~ "seg_black_vhi",
    ## latino ## 
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="vli" & 
      int_jt_relf_a_2020 == 1 ~ "int_hl_vli",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 1 ~ "int_hl_li",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 1 ~ "int_hl_li",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="minc" & 
      int_jt_relf_a_2020 == 1 ~ "int_hl_mi",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="hmi" & 
      int_jt_relf_a_2020 == 1 ~ "int_hl_hmi",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="hinc" & 
      int_jt_relf_a_2020 == 1 ~ "int_hl_hi",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="vhi" & 
      int_jt_relf_a_2020 == 1 ~ "int_hl_vhi",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="vli" & 
      int_jt_relf_a_2020 == 0 ~ "seg_hl_vli",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_hl_li",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_hl_li",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="minc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_hl_mi",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="hmi" & 
      int_jt_relf_a_2020 == 0 ~ "seg_hl_hmi",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="hinc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_hl_hi",
    maj_race_2020 == "per_hl" & 
      maj_ses_2020=="vhi" & 
      int_jt_relf_a_2020 == 0 ~ "seg_hl_vhi",
    ## white ## 
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="vli" & 
      int_jt_relf_a_2020 == 1 ~ "int_white_vli",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 1 ~ "int_white_li",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 1 ~ "int_white_li",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="minc" & 
      int_jt_relf_a_2020 == 1 ~ "int_white_mi",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="hmi" & 
      int_jt_relf_a_2020 == 1 ~ "int_white_hmi",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="hinc" & 
      int_jt_relf_a_2020 == 1 ~ "int_white_hi",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="vhi" & 
      int_jt_relf_a_2020 == 1 ~ "int_white_vhi",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="vli" & 
      int_jt_relf_a_2020 == 0 ~ "seg_white_vli",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_white_li",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="linc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_white_li",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="minc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_white_mi",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="hmi" & 
      int_jt_relf_a_2020 == 0 ~ "seg_white_hmi",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="hinc" & 
      int_jt_relf_a_2020 == 0 ~ "seg_white_hi",
    maj_race_2020 == "per_white" & 
      maj_ses_2020=="vhi" & 
      int_jt_relf_a_2020 == 0 ~ "seg_white_vhi")) %>%
  group_by(int_jt_relf_a_2020,group) %>%
  summarize(n = n()) %>%
  mutate(share = n/nrow(af.wide.final) * 100)

## top 10 in 2000 ##

groups.t10.2000 <- groups.2000 %>%
  ungroup() %>%
  select(-int_jt_relf_a_2000) %>%
  mutate(tot = sum(n),
         share = n/tot) %>%
  arrange(desc(n)) %>%
  slice(1:10) %>%
  mutate(year = "2000")

sum(groups.t10.2000$n)/nrow(af.wide.final)

## top 10 in 2010 ##

groups.t10.2010 <- groups.2010 %>%
  ungroup() %>%
  select(-int_jt_relf_a_2010) %>%
  mutate(tot = sum(n),
         share = n/tot) %>%
  arrange(desc(n)) %>%
  slice(1:10) %>%
  mutate(year = "2010")

sum(groups.t10.2010$n)/nrow(af.wide.final)

## top 10 in 2020 ##

groups.t10.2020 <- groups.2020 %>%
  ungroup() %>%
  select(-int_jt_relf_a_2020) %>%
  mutate(tot = sum(n),
         share = n/tot) %>%
  arrange(desc(n)) %>%
  slice(1:10) %>%
  mutate(year = "2020")

sum(groups.t10.2020$n)/nrow(af.wide.final)

## bring it all together ##

groups.all <- rbind(groups.t10.2000,
                    groups.t10.2010,
                    groups.t10.2020) %>%
  mutate(num = case_when(nchar(n) == 3 ~ as.character(n),
                         nchar(n) == 4 ~ paste0(substr(as.character(n),1,1),
                                               ",",substr(as.character(n),2,4)),
                         nchar(n) == 5 ~ paste0(substr(as.character(n),1,2),
                                                ",",substr(as.character(n),3,5))),
         gr_show = ifelse(n > 3000, num, ""),
         lab = case_when(group == "int_black_li" ~ "Integrated - Black LI",
                         group == "int_hl_hi" ~ "Integrated - Hispanic/Latino HI",
                         group == "int_hl_li" ~ "Integrated - Hispanic/Latino LI",
                         group == "int_white_li" ~ "Integrated - White LI",
                         group == "int_white_hi" ~ "Integrated - White HI",
                         group == "seg_black_li" ~ "Segregated - Black LI",
                         group == "seg_hl_li" ~ "Segregated - Hispanic/Latino LI",
                         group == "seg_white_li" ~ "Segregated - White LI",
                         group == "seg_white_hi" ~ "Segregated - White HI",
                         group == "seg_black_hi" ~ "Segregated - Black HI",
                         group == "seg_asian_hi" ~ "Segregated - Asian HI",
                         group == "seg_hl_hi" ~ "Segregated - Hispanic/Latino HI",
                         group == "seg_asian_li" ~ "Segregated - Asian LI"),
         lab_fin = factor(lab,
                             levels = c("Integrated - Black LI",
                                        "Integrated - Hispanic/Latino LI",
                                        "Integrated - Hispanic/Latino HI",
                                        "Integrated - White LI",
                                        "Integrated - White HI",
                                        "Segregated - Asian HI",
                                        "Segregated - Asian LI",
                                        "Segregated - Black HI",
                                        "Segregated - Black LI",
                                        "Segregated - Hispanic/Latino HI",
                                        "Segregated - Hispanic/Latino LI",
                                        "Segregated - White LI",
                                        "Segregated - White HI")))
  
## plot ##

ggplot(groups.all, aes(fill=lab_fin, y=n, x=year, label = gr_show)) + 
  geom_bar(position="stack", stat="identity") + 
  geom_text(size = 3, position = position_stack(vjust = 0.5)) + 
  xlab("") +
  ylab("") + 
  scale_fill_manual(name = "",
                    values=c("darkolivegreen1",
                             "lightblue1",
                             "skyblue",
                             "salmon",
                             "palevioletred",
                             "gold",
                             "orange",
                             "green3",
                             "seagreen",
                             "dodgerblue",
                             "royalblue",
                             "firebrick",
                             "darkred")) +
  theme_minimal() + 
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_blank())


## make sense of these categories ## 

seg.vli <- af.wide.final %>%
  filter(int_jt_relf_a_2020 == 0 & 
           maj_race_2020 == "per_white" & 
           maj_ses_2020 == "vli") %>%
  group_by(int_jt_relf_a_2000,
           int_jt_relf_a_2010,
           int_jt_relf_a_2020,
           maj_ses_2000,
           maj_ses_2010,
           maj_ses_2020,
           maj_race_2000,
           maj_race_2010,
           maj_race_2020) %>%
  summarize(n = n())


former.seg.vhi <- af.wide.final %>%
  filter(int_jt_relf_a_2000 == 0 & 
           maj_race_2000 == "per_white" & 
           maj_ses_2000 == "vhi") %>%
  group_by(int_jt_relf_a_2000,
           int_jt_relf_a_2010,
           int_jt_relf_a_2020,
           maj_ses_2000,
           maj_ses_2010,
           maj_ses_2020,
           maj_race_2000,
           maj_race_2010,
           maj_race_2020) %>%
  summarize(n = n())


## diff shares of all neighborhoods ##

## white share of all segregated neighborhoods in 2000 ##
sum(groups.2000$n[groups.2000$group %in% c("seg_white_vli",
                                          "seg_white_li",
                                          "seg_white_mi",
                                          "seg_white_hmi",
                                          "seg_white_hi",
                                         "seg_white_vhi")])/sum(af.wide.final$int_jt_relf_a_2000 == 0)

## white share of all integrated neighborhoods in 2000 ##
sum(groups.2000$n[groups.2000$group %in% c("int_white_vli",
                                           "int_white_li",
                                           "int_white_mi",
                                           "int_white_hmi",
                                           "int_white_hi",
                                           "int_white_vhi")])/sum(af.wide.final$int_jt_relf_a_2000 == 1)

## white share of all segregated neighborhoods in 2010 ##
sum(groups.2010$n[groups.2010$group %in% c("seg_white_vli",
                                           "seg_white_li",
                                           "seg_white_mi",
                                           "seg_white_hmi",
                                           "seg_white_hi",
                                           "seg_white_vhi")])/sum(af.wide.final$int_jt_relf_a_2010 == 0)

## white share of all integrated neighborhoods in 2010 ##
sum(groups.2010$n[groups.2010$group %in% c("int_white_vli",
                                           "int_white_li",
                                           "int_white_mi",
                                           "int_white_hmi",
                                           "int_white_hi",
                                           "int_white_vhi")])/sum(af.wide.final$int_jt_relf_a_2010 == 1)

## white share of all segregated neighborhoods in 2020 ##
sum(groups.2020$n[groups.2020$group %in% c("seg_white_vli",
                                           "seg_white_li",
                                           "seg_white_mi",
                                           "seg_white_hmi",
                                           "seg_white_hi",
                                           "seg_white_vhi")])/sum(af.wide.final$int_jt_relf_a_2020 == 0)

## white share of all integrated neighborhoods in 2020 ##
sum(groups.2020$n[groups.2020$group %in% c("int_white_vli",
                                           "int_white_li",
                                           "int_white_mi",
                                           "int_white_hmi",
                                           "int_white_hi",
                                           "int_white_vhi")])/sum(af.wide.final$int_jt_relf_a_2020 == 1)


prop.table(table(af.wide.final$maj_race_2000,useNA = "ifany"))
prop.table(table(af.wide.final$maj_race_2010,useNA = "ifany"))
prop.table(table(af.wide.final$maj_race_2020,useNA = "ifany"))

## what share of int neighborhoods in 2020 are white li/hi and hl li neighborhoods? ##
top.int.2020 <- groups.2020 %>%
  filter(int_jt_relf_a_2020 == 1) %>%
         group %in% c("int_white_hi",
                      "int_white_li",
                      "int_hl_li"))

## calculate ##
sum(top.int.2020$n)/sum(groups.2020$n[groups.2020$int_jt_relf_a_2020==1])


### END OF PROGRAM ###

sink()
sink(type = "message")
